<template>
  <div class="path">
    <header>
      <van-nav-bar
        title="收获地址"
        left-arrow
        @click-left="goback"
      ></van-nav-bar>
    </header>
    <section>
      <div
        class="item"
        v-for="(item, index) in list"
        :key="item.id"
        @click="pathDetail(index)"
      >
        <div class="item-top">
          <p>{{ item.name }}</p>
          <div class="item-phone">
            <p>{{ item.tel | newTel(item.tel) }}</p>
            <span v-if="item.isDefault === 'true'">[默认]</span>
          </div>
          <div class="delete" @click.stop="deleteAddressFn(item.id)">删除</div>
        </div>
        <div class="item-content" @click.stop="updataPath(index)">
          <div>
            <p>{{ item.province }}{{ item.city }}{{ item.county }}</p>
            <p>{{ item.addressDetail }}</p>
          </div>
          <div>
            <i class="iconfont icon-right"></i>
          </div>
        </div>
      </div>
    </section>
    <footer @click="addPath">新建地址</footer>
  </div>
</template>

<script>
import http from '@/serveies/api/request'
import { mapMutations, mapState, mapActions } from 'vuex'
import bus from '@/serveies/bus'
export default {
  name: 'AddPath',
  data () {
    return {}
  },
  created () {
    this.getData()
  },
  computed: {
    ...mapState({
      list: (state) => state.path.list
    })
  },
  filters: {
    newTel (tel) {
      var reg = /^(\d{3})\d{4}(\d{4})$/
      return tel.replace(reg, '$1****$2')
    }
  },
  methods: {
    ...mapMutations(['initGetData']),
    ...mapActions(['deleteAddressFn']),
    getData () {
      http
        .$axios({
          url: '/api/selectAddress',
          method: 'POST',
          headers: {
            token: true
          }
        })
        .then((res) => {
          this.initGetData(res.data)
        })
    },
    goback () {
      this.$router.back()
    },
    updataPath (index) {
      this.$router.push({
        path: '/addPath',
        query: {
          data: JSON.stringify(this.list[index])
        }
      })
    },
    addPath () {
      this.$router.push('/addPath')
    },
    pathDetail (index) {
      this.$router.back()
      bus.$emit('selectPathItem', this.list[index])
    }
  }
}
</script>

<style lang="scss" scoped>
.path {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  header {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    background-color: #eee;
    .van-nav-bar {
      background-color: #eee;
    }
    ::v-deep .van-nav-bar .van-icon {
      color: #333;
      font-size: 0.533333rem;
    }
  }
  section {
    width: 100%;
    height: 100%;
    padding-bottom: 52px;
    padding-top: 46px;
    .item {
      margin: 0.266667rem;
      padding: 0.266667rem;
      border: 1px solid #eee;
      font-size: 0.373333rem;
      color: #666;
      .item-top {
        display: flex;
        justify-content: space-between;
        padding: 5px;
        border-bottom: 1px solid #eee;
        .item-phone {
          display: flex;
          span {
            padding-left: 5px;
            color: #ff6700;
          }
        }
        .delete {
          color: #999;
        }
      }
      .item-content {
        display: flex;
        justify-content: space-between;
        padding: 5px;
        align-items: center;
      }
    }
  }
  footer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: 52px;
    line-height: 52px;
    background-color: #ff6700;
    color: #fff;
    text-align: center;
    font-size: 0.426667rem;
  }
}
</style>
